/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package christieprogram;

import java.awt.Label;
import java.awt.List;
import java.io.File;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import javax.swing.plaf.basic.BasicDirectoryModel;

/**
 *
 * @author Alex
 */
public class ChristieProgramForm extends javax.swing.JFrame {

    private Dictionary<String, String> FilePaths;
    private JFileChooser FileChooser;
    private DefaultListModel<String> lmodel = new DefaultListModel<>();

    /**
     * Creates new form ChristieProgramForm
     */
    public ChristieProgramForm() {
        initComponents();
        jList1.setModel(lmodel);
        FilePaths = new Hashtable<>();
        FileChooser = new JFileChooser();
        ActionCommands.removeAllItems();
        ActionCommandSetup();
    }

    private void ActionCommandSetup() {
        for (ActionEnums Action : ActionEnums.values()) {
            ActionCommands.addItem(Action);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar1 = new java.awt.MenuBar();
        File = new java.awt.Menu();
        menu2 = new java.awt.Menu();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        FilesSelectedLabel = new javax.swing.JLabel();
        ActionCommands = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        File.setLabel("File");
        menuBar1.add(File);

        menu2.setLabel("Edit");
        menuBar1.add(menu2);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Campaign 2013- Excel Formatter");

        jButton1.setText("Select Excel File");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Perform Action");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        FilesSelectedLabel.setText("Selected Files");

        ActionCommands.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("Actions");

        jMenu1.setText("File");

        jMenuItem1.setText("Help");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(ActionCommands, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addComponent(jLabel1)))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(44, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(FilesSelectedLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(180, 180, 180))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jButton1)
                        .addGap(33, 33, 33)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ActionCommands, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(FilesSelectedLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(115, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        FileChooser.showDialog(this, null);
        if (FilePaths.get(FileChooser.getSelectedFile().getName()) == null) {
            FilePaths.put(FileChooser.getSelectedFile().getName(), FileChooser.getSelectedFile().getPath());
            lmodel.addElement(FileChooser.getSelectedFile().getName());
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ActionEnums Comand = ActionEnums.valueOf(ActionCommands.getSelectedItem().toString());

        switch (Comand) {
            case Compile_Finances:
                CompileFinaces();
                break;
            case Find_Duplicates:
                FindDuplicates();
                break;
            case Merge_Files:
                MergeFiles();
                break;
            case Format_Merge:
                FormatMerge();
                break;
            case Load_File:
                LoadFile();
                break;
            case Make_Proper:
                MakeProper();
                break;


        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JOptionPane.showMessageDialog(rootPane, "If you have any issues please log on to MostAwesome."
                + "MyJetbrains.Com\nUsername: Christie\nPassword: christie2013\nCreate Issue in the Campaign-2013 "
                + "Project on the site and I will try to get to "
                + "it as soon as possible");
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChristieProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChristieProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChristieProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChristieProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChristieProgramForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ActionCommands;
    private java.awt.Menu File;
    private javax.swing.JLabel FilesSelectedLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Menu menu2;
    private java.awt.MenuBar menuBar1;
    // End of variables declaration//GEN-END:variables

    private void CompileFinaces() {
        try {
            File file;
            if ((file = selectedFile()) != null) {
                ChristieProgram.compile_finances(file);
            }
        } catch (Exception ex) {
            Logger.getLogger(ChristieProgramForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void FindDuplicates() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void FormatMerge() { // needs returned file names
        int[] selected = jList1.getSelectedIndices();
        if (selected.length < 2 || selected.length > 2) {
            JOptionPane.showMessageDialog(this, "You must select only 2 files to merge");
            return;
        }
//           ChristieProgram.f(fileCreator(lmodel.get(selected[0])), fileCreator(lmodel.get(selected[1])));
    }

    private void LoadFile() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void MakeProper() {
        try {
            File file;
            if ((file = selectedFile()) != null) {
                ChristieProgram.format_file(file, true);
            }
        } catch (Exception ex) {
            Logger.getLogger(ChristieProgramForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private File fileCreator(String path) {
        return new File(FilePaths.get(path));
    }

    private void MergeFiles() {
        int[] selected = jList1.getSelectedIndices();
        if (selected.length < 2 || selected.length > 2) {
            JOptionPane.showMessageDialog(this, "You must select only 2 files to merge");
            return;
        }
        ChristieProgram.merge_files(fileCreator(lmodel.get(selected[0])), fileCreator(lmodel.get(selected[1])));
    }

    public File selectedFile() {
        if (jList1.getSelectedIndex() == -1) {
            JOptionPane.showMessageDialog(rootPane, "You must select a file from the files list");
        } else if (jList1.getSelectedIndices().length > 1) {
            JOptionPane.showMessageDialog(rootPane, "Only a single file may be selected");
        } else {
            return fileCreator(lmodel.get(jList1.getSelectedIndex()));
        }
        return null;
    }
}
